package com.useeinfo.asg.modules.security.entity;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.framework.extend.entity.DataEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import net.sf.json.JSONObject;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;
import java.util.*;

/**
 */
@Entity
@Table(name = "system_role")
public class SystemRole extends DataEntity<SystemRole> {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;					// 主键编号

	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;			// 创建记录时间

	@Column(columnDefinition = "bit default 0")
	private boolean deleteStatus;		// 逻辑删除状态    0.否    1.是
	private String roleName;
	private String roleDescription;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "system_role_2_authority", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "authority_id"))
	private Set<SystemAuthority> authoritySet;
	private boolean systemManager;
	private boolean enabled;

	@ManyToMany(mappedBy="systemRoleSet")
	private Set<SystemUser> userSet = new HashSet<>();

	@Override
	public JSONObject toJSONObject() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("roleId", getId());
		jsonObject.put("createTime", CommUtil.formatLongDate(this.getCreateTime()));
		jsonObject.put("roleName", CommonSugar.getTypedDefault(getRoleName(), ""));
		jsonObject.put("roleDescription", CommonSugar.getTypedDefault(getRoleDescription(), ""));
		jsonObject.put("systemManager", isSystemManager());
		jsonObject.put("enabled", isEnabled());

		return jsonObject;
	}

	public SystemRole() {
	}
	public SystemRole(Long roleId) {
		this.id = roleId;
	}

	public Set<SystemUser> getUserSet() {
		return userSet;
	}

	public void setUserSet(Set<SystemUser> userSet) {
		this.userSet = userSet;
	}

	@Override
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public boolean isDeleteStatus() {
		return deleteStatus;
	}

	public void setDeleteStatus(boolean deleteStatus) {
		this.deleteStatus = deleteStatus;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleDescription() {
		return roleDescription;
	}

	public void setRoleDescription(String roleDescription) {
		this.roleDescription = roleDescription;
	}

		public Set<SystemAuthority> getAuthoritySet() {
		return authoritySet;
	}

	public void setAuthoritySet(Set<SystemAuthority> authoritySet) {
		this.authoritySet = authoritySet;
	}

	public boolean isSystemManager() {
		return systemManager;
	}

	public void setSystemManager(boolean systemManager) {
		this.systemManager = systemManager;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void addAuthority(SystemAuthority systemAuthority) {
		this.authoritySet.add(systemAuthority);
	}

	public void removeAuthority(SystemAuthority systemAuthority) {
		this.authoritySet.remove(systemAuthority);
	}
}
